<?php


namespace app\common\model;


use think\facade\Cache;
use think\Model;
use think\model\concern\SoftDelete;

class WebConfig extends Model
{
    /**
     * 读取配置类型
     * @return array
     */
    public static function getTypeList()
    {
        $typeList = [
            'string'   => '字符',
            'text'     => '文本',
            'editor'   => '编辑器',
            'number'   => '数字',
            'date'     => '日期',
            'time'     => '时间',
            'datetime' => '日期时间',
            'select'   => '列表',
            'selects'  => '列表多选',
            'image'    => '单图',
            'images'   => '多图',
            'file'     => '单文件',
            'files'    => '多文件',
            'switch'   => '开关',
            'checkbox' => '复选',
            'radio'    => '单选',
            'array'    => '数组',
        ];
        return $typeList;
    }

    public static function getRegexList()
    {
        $regexList = [
            'required' => '必选',
            'digits'   => '数字',
            'letters'  => '字母',
            'date'     => '日期',
            'time'     => '时间',
            'email'    => '邮箱',
            'url'      => '网址',
            'qq'       => 'QQ号',
            'IDcard'   => '身份证',
            'tel'      => '座机电话',
            'mobile'   => '手机号',
            'zipcode'  => '邮编',
            'chinese'  => '中文',
            'username' => '用户名',
            'password' => '密码'
        ];
        return $regexList;
    }

    /**
     * 读取分类分组列表
     * @return array
     */
    public static function getGroupList()
    {
        $data = self::where(['group' => 'dictionary', 'name' => 'config_group'])->find();
        $value = $data['value'];
        return json_decode($value, true);
    }
    /**
     * 将字符串解析成键值数组
     * @param string $text
     * @return array
     */
    public static function decode($text, $split = "\r\n")
    {
        $content = explode($split, $text);
        $arr = [];
        foreach ($content as $k => $v) {
            if (stripos($v, "|") !== false) {
                $item = explode('|', $v);
                $arr[$item[0]] = $item[1];
            }
        }
        return $arr;
    }

    public static function getWebConfig($type = 'basic')
    {
        $cacheName = $type . '_config_cache';
        $sqlCache = $type . '_cache';
        $basicConfig = Cache::get($cacheName);
        if (empty($basicConfig)) {
            $basic = WebConfig::where(['group' => [$type]])->cache($sqlCache, 360)->select()->toArray();
            $basicConfig = [];
            foreach ($basic as $k => $v) {
                $basicConfig[$v['name']] = $v['value'];
            }
            Cache::set($cacheName, $basicConfig);
        }
        return $basicConfig;
    }
}
